package com.duguo.dynamicmvc.factory.stage;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import com.duguo.dynamicmvc.factory.ControllerFactory;
import com.duguo.dynamicmvc.factory.WorkingModel;
import com.duguo.dynamicmvc.factory.utils.BeanUtils;

public class RegisterDynamicControllerStage implements Stage,BeanFactoryAware {
	BeanFactory _beanFactory;
	
    public void execute(ControllerFactory controllerFactory) throws Exception {
    	WorkingModel workingModel = controllerFactory.getWorkingModel();
		Object controllerInstance = workingModel.getControllerInstance();
		if(workingModel.isMultiActionController())
    		BeanUtils.setFieldValue(controllerInstance, "registedControllers", workingModel.getRegistedControllers());
        String dynamicControllerName = workingModel.getDynamicControllerName();
		if (dynamicControllerName != null) {
            Object dynamicController = _beanFactory.getBean(dynamicControllerName);
            BeanUtils.setFieldValue(controllerInstance, "dynamicController", dynamicController);
        }
    }

	public void setBeanFactory(BeanFactory beanFactory) {
		_beanFactory = beanFactory;
	}

}
